/*
 * jlb
 */
package com.jlb.news.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baidu.fsg.uid.UidGenerator;
import com.jlb.core.base.BaseDAO;
import com.jlb.core.base.BaseSVImpl;
import com.jlb.core.entity.Page;
import com.jlb.core.exceptions.BaseException;
import com.jlb.core.exceptions.NewsException;
import com.jlb.news.dao.NewsContentDAO;
import com.jlb.news.dao.NewsDAO;
import com.jlb.news.entity.News;
import com.jlb.news.entity.NewsContent;
import com.jlb.news.entity.NewsState;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("newsSV")
@Slf4j
public class NewsSVImpl extends BaseSVImpl<News, Long> implements NewsSV {

    @Autowired
    private NewsDAO newsDAO;

    @Autowired
    private NewsContentDAO newsContentDAO;

    @Resource
    private UidGenerator uidGenerator;

    @Override
    protected BaseDAO getBaseDAO() {
        return newsDAO;
    }

    /**
     * 保存account对象
     *
     * @param entity 实体
     * @throws BaseException
     */
    @Override
    public void save(News entity) throws BaseException {
        String code = String.valueOf(uidGenerator.getUID());
        entity.setCode(code);
        entity.setState(NewsState.Released.name());
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        super.save(entity);


    }

    @Override
    public void insert(News news, String content) {
        String code = String.valueOf(uidGenerator.getUID());
        news.setCode(code);
        news.setState(NewsState.Released.name());
        news.setCreateTime(new Date());
        news.setUpdateTime(new Date());
        super.save(news);

        //保存新闻公告内容
        NewsContent newsContent = new NewsContent();
        newsContent.setNewsCode(code);
        newsContent.setContent(content);
        newsContentDAO.insert(newsContent);
    }

    @Override
    public void update(News news, String content) {
        super.modify(news);
        //修改新闻公告内容
        NewsContent newsContent = newsContentDAO.loadByNewsCode(news.getCode());
        newsContent.setContent(content);
        newsContentDAO.update(newsContent);
    }

    /**
     * 加载一个对象News
     *
     * @param id * @param code 编码
     * @return News
     */
    @Override
    public News load(java.lang.Long id, java.lang.String code) {
        if (id == null && code == null) {
            throw new NewsException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }

        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return newsDAO.load(param);
    }

    /**
     * 加载一个对象News 通过id
     *
     * @param id
     * @return News
     */
    @Override
    public News loadById(java.lang.Long id) {
        if (id == null) {
            throw new NewsException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return newsDAO.loadById(id);
    }

    /**
     * 加载一个对象News 通过code
     *
     * @param code 编码
     * @return News
     */
    @Override
    public News loadByCode(java.lang.String code) {
        if (code == null) {
            throw new NewsException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        return newsDAO.loadByCode(code);
    }


    /**
     * 根据主键id,oldStates 共同更新 News 的状态到newState状态
     *
     * @param id
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateById(java.lang.Long id, NewsState newState, NewsState... oldStates) {
        if (id == null) {
            throw new NewsException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new NewsException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new NewsException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        newsDAO.updateStateById(id, new Date(), newState, oldStates);
    }

    /**
     * 根据主键code,oldStates 共同更新 News 的状态到newState状态
     *
     * @param code      编码
     * @param newState  新状态
     * @param oldStates 旧状态集合
     */
    @Override
    public void updateStateByCode(java.lang.String code, NewsState newState, NewsState... oldStates) {
        if (code == null) {
            throw new NewsException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (newState == null) {
            throw new NewsException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        if (oldStates == null) {
            throw new NewsException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        newsDAO.updateStateByCode(code, new Date(), newState, oldStates);
    }

    /**
     * 根据主键id 更新 News 的状态到另一个状态
     *
     * @param id
     * @param state 状态
     */
    @Override
    public void updateById(java.lang.Long id, NewsState state) {
        if (id == null) {
            throw new NewsException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        newsDAO.updateById(id, state, new Date());
    }

    /**
     * 根据主键code 更新 News 的状态到另一个状态
     *
     * @param code  编码
     * @param state 状态
     */
    @Override
    public void updateByCode(java.lang.String code, NewsState state) {
        if (code == null) {
            throw new NewsException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        newsDAO.updateByCode(code, state, new Date());
    }


    /**
     * 删除对象News
     *
     * @param id * @param code 编码
     * @return News
     */
    @Override
    public void delete(java.lang.Long id, java.lang.String code) {
        if (id == null && code == null) {
            throw new NewsException(BaseException.BaseExceptionEnum.Ilegal_Param);
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        newsDAO.delete(param);
    }


    /**
     * 查询News分页
     *
     * @param news   对象
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<News>
     */
    @Override
    public List<News> list(News news, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }

        Map<String, Object> map = null;
        if (news != null) {
            map = JSON.parseObject(JSON.toJSONString(news, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return newsDAO.list(map, new RowBounds(offset, limit));
    }

    @Override
    public int count(News news) {
        Map<String, Object> map = null;
        if (news != null) {
            map = JSON.parseObject(JSON.toJSONString(news, SerializerFeature.WriteDateUseDateFormat));
        } else {
            map = new HashMap<>();
        }
        return newsDAO.count(map);
    }

    /**
     * 查询News分页
     *
     * @param id     * @param code  编码
     * @param offset 查询开始行
     * @param limit  查询行数
     * @return List<News>
     */
    @Override
    public List<News> list(java.lang.Long id, java.lang.String code, int offset, int limit) {
        if (offset < 0) {
            offset = 0;
        }

        if (limit < 0) {
            limit = Page.limit;
        }
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return newsDAO.list(param, new RowBounds(offset, limit));
    }

    @Override
    public int count(java.lang.Long id, java.lang.String code) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("code", code);
        return newsDAO.count(param);
    }

}
